package net.magicalcloud.magicalcloud.view.viewImpl.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.widget.Toast;

import com.aurelhubert.ahbottomnavigation.AHBottomNavigation;
import com.aurelhubert.ahbottomnavigation.AHBottomNavigationAdapter;
import com.aurelhubert.ahbottomnavigation.AHBottomNavigationViewPager;

import net.magicalcloud.magicalcloud.R;
import net.magicalcloud.magicalcloud.adapter.TabbarViewPageAdapter;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 这里是View层 v层只负责显示数据，这里应当只接受处理好的数据，数据的处理在p层
 */

public class MainActivity extends AppCompatActivity{


    @BindView(R.id.bottom_prime)
    AHBottomNavigation bottomPrime;
    @BindView(R.id.bottom_minory)
    AHBottomNavigation bottomMinory;
    @BindView(R.id.view_pager)
    AHBottomNavigationViewPager viewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initView();
    }

    void initView(){

        AHBottomNavigationAdapter navigationAdapter1 = new AHBottomNavigationAdapter(this, R.menu.bottom_navigation_menu_3);
        navigationAdapter1.setupWithBottomNavigation(bottomPrime);
        //设置底部导航的背景颜色
        bottomPrime.setDefaultBackgroundColor(Color.parseColor("#ffffff"));
        //被选中的颜色
        bottomPrime.setAccentColor(Color.parseColor("#fda3b2"));
        //没有被选中的颜色
        bottomPrime.setInactiveColor(Color.parseColor("#d2d8e5"));
        bottomPrime.setTitleState(AHBottomNavigation.TitleState.ALWAYS_SHOW);
        bottomPrime.setBehaviorTranslationEnabled(false);
        bottomPrime.setOnTabSelectedListener(new AHBottomNavigation.OnTabSelectedListener() {
            @Override
            public boolean onTabSelected(int position, boolean wasSelected) {
                viewPager.setCurrentItem(position, false);
                return true;
            }
        });

        viewPager.setOffscreenPageLimit(3);
        TabbarViewPageAdapter adapter = new TabbarViewPageAdapter(getSupportFragmentManager());
        viewPager.setAdapter(adapter);

    }


    long exitTime=0;// 退出时间
    /** 捕捉按下返回键操作 */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            // 判断间隔时间 大于2秒就退出应用
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                String msg = "再按一次返回键退出";
                Toast.makeText(this,msg,Toast.LENGTH_SHORT).show();
                // 计算两次返回键按下的时间差
                exitTime = System.currentTimeMillis();
            } else {
                // 关闭应用程序
                finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


}
